package com.lxit.video.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.lxit.video.bean.VideoComment;
import com.lxit.video.service.VideoCommentService;
import com.lxit.video.util.LayuiPageUtil;
import com.lxit.video.util.PageUtil;

@Controller
@RequestMapping("/VideoComment/")
public class VideoCommentController {
	@Autowired
	private VideoCommentService videoCommentService;

	/**
	 * @author 贺长全
	 * @time 2018 下午8:23:02
	 * @param videoId
	 *            视频Id
	 * @param commentDetails
	 *            视频详情;
	 * @return
	 * @desc ajax 执行添加操作;
	 */

	@ResponseBody
	@RequestMapping(value = "saveVideoComment", produces = "text/plain;charset=UTF-8")
	public String saveVideoComment(Integer videoId, String commentDetails,HttpSession session) {
		// 评论用户id;
		// UserInfo user = (UserInfo) session.getAttribute(""); user.getUserId();
		boolean bool = videoCommentService.saveVideoComment(1, commentDetails, videoId);
		if (bool == false) {
			videoId = -1;
		}
		return JSONObject.toJSONString(videoId);
	}

	/**
	 * @author 贺长全
	 * @time 2018 下午8:46:58
	 * @param commentId
	 *            评论id;
	 * @return
	 * @desc 后台修改评论状态
	 */
	@ResponseBody
	@RequestMapping(value = "updateVideoComment")
	public String updateVideoComment(Integer commentId) {
		if (videoCommentService.updateVideoComment(commentId)) {
			return "true";
		}
		return "false";
	}

	/**
	 * @author 贺长全
	 * @time 2018 下午8:56:28
	 * @param pageIndex
	 * @param pageSize
	 * @param userNumber
	 * @param videoName
	 * @param stratTime
	 * @param entTime
	 * @return 后台分页已json格式接收;
	 * @desc 后台分页;
	 */
	@ResponseBody
	@RequestMapping(value = "pageBackstageVideoComment")
	public Object pageBackstageVideoComment(@RequestParam(value = "page") Integer pageIndex,
			@RequestParam(value = "limit") Integer pageSize, String userNumber, String videoName, String stratTime,
			String endTime) {
		PageUtil<VideoComment> pageUtil = videoCommentService.pageBackstageVideoComment(pageIndex, pageSize, userNumber,
				videoName, stratTime, endTime);
		Object json = null;
		if(pageUtil!=null) {
			LayuiPageUtil<VideoComment> layuiPageUtil = new LayuiPageUtil<>(pageUtil.getTotal(), pageUtil.getRows());
			json = JSONObject.toJSON(layuiPageUtil);
		}
		return json;
	}

	/**
	 * @author 贺长全
	 * @time 2018 下午8:58:47
	 * @param pageIndex
	 * @param pageSize
	 * @param videoId
	 * @return
	 * @desc 前台评论分页;
	 */
	@RequestMapping(value = "pageVideoComment")
	public String pageVideoComment(Integer pageIndex, Integer pageSize, Integer videoId, Map<String, Object> map) {
		PageUtil<VideoComment> pageUtil = videoCommentService.pageVideoComment(pageIndex, pageSize, videoId);
		map.put("pageUtil", pageUtil);
		return "";
	}
}